﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class DCompany
    {
        public bool Exists(string roleName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tcompany");
            strSql.Append(" where CompanyName=@CompanyName");
            MySqlParameter[] parameters = {
					new MySqlParameter("@CompanyName", MySqlDbType.VarChar,30)
          };
            parameters[0].Value = roleName;
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds==null||ds.Tables.Count==0||ds.Tables[0].Rows.Count==0)
            {
                return false;
            }
            int result = int.Parse(ds.Tables[0].Rows[0][0].ToString());
            return result > 0;
        }
 
         
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.CompanyInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tcompany(");
            strSql.Append("CompanyName,Remark)");
            strSql.Append(" values (");
            strSql.Append("@CompanyName,@Remark)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@CompanyName", MySqlDbType.VarChar,30),				 
					new MySqlParameter("@Remark", MySqlDbType.Text)};
            parameters[0].Value = model.Name;            
            parameters[1].Value = model.Remark;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.CompanyInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tcompany set ");
            strSql.Append("CompanyName=@CompanyName,");       
            strSql.Append("Remark=@Remark");
            strSql.Append(" where Id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@CompanyName", MySqlDbType.VarChar,30),			 
					new MySqlParameter("@Remark", MySqlDbType.Text),
					new MySqlParameter("@id", MySqlDbType.Int32,10)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Remark;            
            parameters[2].Value = model.Id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tcompany ");
            strSql.Append(" where Id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.CompanyInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,CompanyName,Remark from tcompany ");
            strSql.Append(" where Id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            Model.CompanyInfo model = new Model.CompanyInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CompanyName"] != null && ds.Tables[0].Rows[0]["CompanyName"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["CompanyName"].ToString();
                }                
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,CompanyName,Remark ");
            strSql.Append(" FROM tcompany ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        public List<string> GetCompanyList()
        {
            DataSet ds = GetList("");
            List<string> list = new List<string>();
            if (ds==null||ds.Tables.Count==0)
            {
                return null;
            }
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                string company = row["CompanyName"].ToString();
                list.Add(company);
            }
            return list;
        }


        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tcompany ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


    }
}
